package com.google.appengine.a4ieve.filters;

import com.google.appengine.a4ieve.Helper;
import com.google.appengine.api.users.UserServiceFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: lunin
 * Date: 4/22/14
 * Time: 5:53 PM
 * Filter purely for maintenance purposes
 */
public class MaintenanceFilter implements Filter {

    private static final String[] ALLOWED_USERS = {"oleh.lunin"};
    private static final String MAINTENANCE_URL = "maintenance.html";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String emailKey = Helper.cropEmailId(UserServiceFactory.getUserService().getCurrentUser().getEmail());
        for (String allowed : ALLOWED_USERS) {
            if (allowed.equals(emailKey)) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
        }
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.sendRedirect(MAINTENANCE_URL);
    }

    @Override
    public void destroy() {

    }
}
